package com.itheima.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class MyGlobalExceptionHandler {

    /**
     * 用来处理账号重复时的异常
     * @param e
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R SQLIntegrity(SQLIntegrityConstraintViolationException e) {

        String s = e.getMessage();

        if (s.contains("Duplicate entry")) {

            String s1 = s.split(" ")[2];

            return R.error(s1 + "已存在!");

        }

        return R.error("未知错误!");

    }
}
